package com.yanqu.road.server.cmd.pageant;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.game.result.UserPageantDataResult;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.pb.PageantPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PAGEANT_BIND_BEAUTY, desc = "绑定美女")
public class PageantBindBeautyCmd extends AbstractCommand {

    private int activityId;

    private int beautyId;

    private UserPageantDataResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.PageantBindBeautyReqMsg reqMsg = PageantProto.PageantBindBeautyReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        beautyId = reqMsg.getBeautyId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        if (activityInfo == null || !ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (player.getModule(BeautyModule.class).getUserBeauty(beautyId) == null) {
            ret = GameErrorCode.E_BEAUTY_NO_FOUND;
            return;
        }

        result = player.getModule(PageantModule.class).initGameData(activityInfo.getActivityId(), beautyId);
    }

    @Override
    public void sendData(GamePlayer player) {
        PageantProto.PageantBindBeautyRespMsg.Builder respMsg = PageantProto.PageantBindBeautyRespMsg.newBuilder();
        if(0==ret) {
            respMsg.setRet(result.getRet());
            if (0 == result.getRet()) {
                if(null!=result.getUserPageantData()) {
                    respMsg.setEntity(PageantPb.parsePageantBaseRespMsg(result.getUserPageantData()));
                }
            }
        }else {
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_PAGEANT_BIND_BEAUTY, respMsg);
    }
}
